package com.neusoft.neuiotms.dcom.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.KnowledgeAttachmentDTO;
import com.neusoft.neuiotms.dcom.model.KnowledgeAttachment;
import com.neusoft.neuiotms.dcom.repository.KnowledgeAttachmentRepository;

@Component
public class KnowledgeAttachmentConverter extends AbstractConverter<KnowledgeAttachment, KnowledgeAttachmentDTO> {

    @Autowired
    private KnowledgeAttachmentRepository knowledgeAttachmentRepository;

    @Override
    public KnowledgeAttachment toModel(final KnowledgeAttachmentDTO dto) {
        KnowledgeAttachment model;
        if (dto.isNew()) {
            model = new KnowledgeAttachment();
        } else {
            model = this.knowledgeAttachmentRepository.getOne(dto.getId());
        }
        model.setFsId(dto.getFsId());
        model.setName(dto.getName());
        return model;
    }

    @Override
    public KnowledgeAttachmentDTO toDTO(final KnowledgeAttachment model, final boolean forListView) {
        final KnowledgeAttachmentDTO dto = new KnowledgeAttachmentDTO();
        dto.setFsId(model.getFsId());
        dto.setId(model.getId());
        dto.setName(model.getName());
        return dto;
    }

}
